<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="头像" size="mini" prop="img">
        <single-upload v-model="dataForm.img"></single-upload>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="dataForm.sex" placeholder="请选择性别">
          <el-option
            v-for="item in options01"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
export default {
  components: {
    SingleUpload,
  },
  data() {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error("密码不能为空"));
      } else {
        callback();
      }
    };
    var validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error("确认密码不能为空"));
      } else if (this.dataForm.password !== value) {
        callback(new Error("确认密码与密码输入不一致"));
      } else {
        callback();
      }
    };
    var validateEmail = (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error("邮箱格式错误"));
      } else {
        callback();
      }
    };
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error("手机号格式错误"));
      } else {
        callback();
      }
    };
    return {
      options01: [
        {
          value: 0,
          label: "男",
        },
        {
          value: 1,
          label: "女",
        },
      ],
      options: [],
      flag: 0,
      defaultProps: {
        children: "children",
        label: "name",
        value: "id",
      },
      menus: [],
      dormValue: "",
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        userName: "",
        email: "",
        mobile: "",
        status: 1,
        num: "",
        dormId: "",
        realName: "",
        roleId: "",
        img: "",
        sex: "",
      },
      // dataRule: {
      //   userName: [
      //     { required: true, message: "用户名不能为空", trigger: "blur" },
      //   ],
      //   password: [{ validator: validatePassword, trigger: "blur" }],
      //   comfirmPassword: [
      //     { validator: validateComfirmPassword, trigger: "blur" },
      //   ],
      //   email: [
      //     { required: true, message: "邮箱不能为空", trigger: "blur" },
      //     { validator: validateEmail, trigger: "blur" },
      //   ],
      //   mobile: [
      //     { required: true, message: "手机号不能为空", trigger: "blur" },
      //     { validator: validateMobile, trigger: "blur" },
      //   ],
      // },
    };
  },
  activated() {},
  created() {},
  methods: {
    handleChange() {},
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$http({
        url: this.$http.adornUrl("/sys/user/info"),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataForm.img = data.data.headImg;
          this.dataForm.userName = data.data.username;
          this.dataForm.email = data.data.email;
          this.dataForm.mobile = data.data.mobile;
          this.dataForm.id = data.data.userId;
          this.dataForm.sex=data.data.sex;
          console.log("创建完成", this.options);
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/user/update`),
            method: "post",
            data: this.$http.adornData({
              userId: this.dataForm.id,
              username: this.dataForm.userName,
              email: this.dataForm.email,
              mobile: this.dataForm.mobile,
              headImg: this.dataForm.img,
              sex: this.dataForm.sex,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
